package com.sfang.common.util.img;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

/**
 * @Author: user
 * @Date: 2020/1/7 15:00
 * @Version: 1.0
 */
public class BufferedImageUtil {

     /**
      * 通过BufferedImage类裁剪图片
      * imgUrl 通过网络图片，裁剪图片
      * x位置
      * y位置
      * w宽
      * h长
      */
     public static BufferedImage getSubimage(String imgUrl,Integer x, Integer y, Integer w, Integer h) throws IOException {

        URL url= new URL(imgUrl);

        BufferedImage bufImage = ImageIO.read(url);

        BufferedImage subimage = bufImage.getSubimage(x, y, w, h);

        return subimage;
    }

    /**
     * BufferedImage转byte[]
     * format ->文件格式
     *
     */
    public static byte[] imageToBytes(BufferedImage bImage, String format) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bImage, format, out);
        return out.toByteArray();
    }

    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public static  InputStream bufferedImageToInputStream(BufferedImage image,String form){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, form, os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @param filePostUrl 请求接口URL
     * @param params	  除文件外其它参数
     * @param filesMap    Map<文件名,文件二进制byte[]>
     * @return
     */
    public static String postFiles(String filePostUrl,Map<String, String> headers,
                                   Map<String, String> params, Map<String, byte[]> filesMap) {

        HttpPost httppost = new HttpPost(filePostUrl);

        for (String key : headers.keySet()) {
            httppost.setHeader(key,headers.get(key));
        }

        HttpClient client = HttpClients.createDefault();
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        // 是否多个文件
        for (String key : filesMap.keySet()) {
            entityBuilder.addBinaryBody("file", filesMap.get(key),
                    ContentType.DEFAULT_BINARY, key);
        }
        // 是否有表单参数
        for (String key : params.keySet()) {
            entityBuilder.addTextBody(key, params.get(key),ContentType.TEXT_PLAIN.withCharset("UTF-8"));
        }

        httppost.setEntity(entityBuilder.build());
        try {
            HttpResponse response = client.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.getConnectionManager().shutdown();
        }
        return null;
    }

}
